package com.qk.service.impl;


import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

   /* {
        "code": 1,
            "msg": "success",
            "data": {
        "clueTotal": 9,
                "clueWaitAllot": 3,
                "clueWaitFollow": 3,
                "clueFollowing": 1,
                "clueFalse": 0,
                "clueConvertBusiness": 2,
                "businessTotal": 2,
                "businessWaitAllot": 2,
                "businessWaitFollow": 0,
                "businessFollowing": 0,
                "businessFalse": 0,
                "businessConvertCustomer": 0
    }
    }*/
    @Override
    public OverviewVo getOverview() {
        //1.查询redis缓存中的数据
        log.info("从redis中,查询redis缓存中 线索和商机 的概览数据");
        Object overview = redisTemplate.opsForValue().get("OVERVIEW_DATA");

        //2.判断数据是否为空
        if (overview != null){
            log.info("从redis中,查询redis缓存中 线索和商机 的概览数据成功");
            return (OverviewVo) overview;
        }

        //1.获取线索对应的数量
        OverviewVo clueOverviewVo =clueMapper.getClueOverview();

        //2.获取商机对应的数量
        OverviewVo businessOverviewVo =businessMapper.getBusinessOverview();

        //3.封装OverviewVo
        BeanUtils.copyProperties(clueOverviewVo,businessOverviewVo, "businessTotal","businessWaitAllot", "businessWaitFollow", "businessFollowing", "businessFalse", "businessConvertCustomer");


        //4.将数据保存到redis中
        log.info("查询到 线索和商机 的概览数据,将数据缓存到redis中");
        redisTemplate.opsForValue().set("OVERVIEW_DATA",businessOverviewVo,1, TimeUnit.MINUTES);

        return businessOverviewVo;
    }
}
